package com.longe.service.impl;

import com.longe.config.MinioConfiguration;
import com.longe.service.MinioService;
import com.longe.util.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Service
public class MinioServiceImpl implements MinioService {

    @Autowired
    private MinioUtil minioUtil;

    @Autowired
    private MinioConfiguration minioConfiguration;

    @Override
    public String putObject(MultipartFile file, String fileType) {

        String fileName = file.getOriginalFilename();
        String objectName = UUID.randomUUID().toString().replaceAll("-", "")
                + fileName.substring(fileName.lastIndexOf("."));
        boolean result = minioUtil.putObject(file, objectName, fileType);
        if (result) {
            return minioConfiguration.getEndpoint() + ":" + minioConfiguration.getPort() + "/" + minioConfiguration.getBucketName() + "/" + objectName;
        }
        return null;
    }

    @Override
    public boolean removeObject(String objectName) {
        return minioUtil.removeObject(objectName);
    }

}
